package com.lagou.homework;

import org.apache.commons.lang.ObjectUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class NumberReducer extends Reducer<NumberBean, NullWritable, NumberBean, NullWritable> {

    /**
     * 因为自定义的NumberBean类实现了WritableComparable接口, 所以会按照升序挨个调用reduce方法; 这里声明一个
     * 变量用来统计当前调用reduce方法的记录的顺序, 并把这个顺序编号写入到NumberBean类的对象中.
     */
    private Integer count = 1;
    @Override
    protected void reduce(NumberBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {

        for (NullWritable value : values) {
            //key代表当前正在调用reduce方法的NumberBean对象, 给bean对象设置id编号值
            key.setId(count++);
            context.write(key, NullWritable.get());
        }
    }
}
